<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@include file="/WEB-INF/jsp/common/taglibs.jspf"%>
<es:contentHeader />
<%@include file="/WEB-INF/jsp/common/import-upload-css.jspf"%>
<div class="panel">

	<ul class="nav nav-tabs">
		<shiro:hasPermission name="wei:circle:create">
			<c:if test="${op eq '新增'}">
				<li ${op eq '新增' ? 'class="active"' : ''}><a
					href="${ctx}/wei/circle/circle/create?BackURL=<es:BackURL/>"> <i
						class="icon-file-alt"></i> 新增
				</a></li>
			</c:if>
		</shiro:hasPermission>

		<c:if test="${not empty m.id}">
			<li ${op eq '查看' ? 'class="active"' : ''}><a
				href="${ctx}/wei/circle/circle/${m.id}?BackURL=<es:BackURL/>"> <i
					class="icon-eye-open"></i> 查看
			</a></li>
			<shiro:hasPermission name="wei:circle:update">
				<li ${op eq '修改' ? 'class="active"' : ''}><a
					href="${ctx}/wei/circle/circle/${m.id}/update?BackURL=<es:BackURL/>">
						<i class="icon-edit"></i> 修改
				</a></li>
			</shiro:hasPermission>
			<shiro:hasPermission name="wei:circle:delete">
				<li ${op eq '删除' ? 'class="active"' : ''}><a
					href="${ctx}/wei/circle/circle/${m.id}/delete?BackURL=<es:BackURL/>">
						<i class="icon-trash"></i> 删除
				</a></li>
			</shiro:hasPermission>
		</c:if>
		<li><a href="<es:BackURL/>" class="btn btn-link"> <i
				class="icon-reply"></i> 返回
		</a></li>
	</ul>

	<form:form id="editForm" method="post" commandName="m" cssClass="form-horizontal">
		<es:showGlobalError commandName="m" />

		<form:hidden path="id" />

		<div class="control-group">
			<form:label path="postTitle" cssClass="control-label">帖子标题</form:label>
			<div class="controls">
				<form:input path="postTitle"
					cssClass="input-xxlarge validate[required]"
					placeholder="5到10个字母、数字、下划线" />
			</div>
		</div>

		<div class="control-group">
			<form:label path="category.name" cssClass="control-label">所属类别</form:label>
			<div class="controls input-append">
				<form:input id="categoryName" path="category.name"
					cssClass="validate[required]" readonly="true" />
				<span class="add-on"><i class="icon-chevron-down"></i></span>
				<form:hidden id="categoryId" path="category.id" />
			</div>
		</div>

		<div class="control-group">
			<form:label path="postContent" cssClass="control-label">帖子内容</form:label>
			<div class="controls">
				<c:choose>
					<c:when test="${op ne '查看'}">
						<form:textarea path="postContent" cssClass="validate[required]"
							cssStyle="width: 550px;height:150px;" />
					</c:when>
					<c:otherwise>
                        ${m.postContent}
                    </c:otherwise>
				</c:choose>
			</div>
		</div>

		<div class="control-group"
			style="margin-bottom: 0px;<c:if test="${empty m.postPic}">display: none</c:if>">
			<label for="files" class="control-label"></label>
			<div class="controls">
				<div class="ajax-upload-view"></div>
				<form:hidden path="postPic" />
			</div>
		</div>

		<div class="control-group">
			<label for="files" class="control-label"> 帖子图片</label>
			<div class="controls">
				<label for="files" class="btn btn-success fileinput-button">
					<i class="icon-plus icon-white"></i> <span>添加文件...</span> <input
					type="file" id="files" name="files[]" data-url="${ctx}/ajaxUpload"
					multiple>
				</label>
			</div>
		</div>

		<div class="control-group">
			<form:label path="postUpCount" cssClass="control-label">被赞数</form:label>
			<div class="controls input-prepend input-append">
				<form:input path="postUpCount"
					cssClass="validate[required,custom[integer]]"
					placeholder="请输入整数且大于等于0的数字" />
			</div>
		</div>

		<div class="control-group">
			<form:label path="show" cssClass="control-label">是否显示</form:label>
			<div class="controls inline-radio">
				<form:radiobuttons path="show" items="${booleanList}"
					itemLabel="info" itemValue="value" cssClass="validate[required]" />
			</div>
		</div>


		<c:if test="${op eq '新增'}">
			<c:set var="icon" value="icon-file-alt" />
		</c:if>
		<c:if test="${op eq '修改'}">
			<c:set var="icon" value="icon-edit" />
		</c:if>
		<c:if test="${op eq '删除'}">
			<c:set var="icon" value="icon-trash" />
		</c:if>

		<div class="control-group">
			<div class="controls">
				<button type="submit" class="btn btn-primary">
					<i class="${icon}"></i> ${op}
				</button>
				<a href="<es:BackURL/>" class="btn"> <i class="icon-reply"></i>
					返回
				</a>
			</div>
		</div>

	</form:form>
</div>
<es:contentFooter />
<%@include file="/WEB-INF/jsp/common/import-editor-js.jspf"%>
<%@include file="/WEB-INF/jsp/common/import-upload-simple-js.jspf"%>
<script type="text/javascript">
	$(function() {
		$('.fileinput-button input[type="file"]').fileupload({
			dataType : "json"
		});
		$('.fileinput-button input[type="file"]')
				.fileupload(
						"option",
						{
							progressall : function(e, data) {
								var view = $(".ajax-upload-view");
								view.parent().parent().show();
								var progressBar = view.find(".progress");
								if (progressBar.size() == 0) {
									var progressBarTemplate = '<div class="progress progress-striped">'
											+ '<div class="bar"></div>'
											+ '</div>';
									progressBar = view
											.append(progressBarTemplate);
								}
								var progress = parseInt(data.loaded
										/ data.total * 100, 10);
								progressBar.find(".bar").css("width",
										progress + "%");
							},
							start : function(e) {
								$(".ajax-upload-view").html("");
								var submitBtn = $(this).closest("form").find(
										":submit");
								submitBtn.data("value", submitBtn.val()).val(
										"上传文件中...").prop("disabled", true);
							},
							//上传完成
							done : function(e, data) {
								$
										.each(
												data.result.files,
												function(index, file) {
													if (file.error) {
														$(".ajax-upload-view")
																.html(
																		"<div class='alert alert-error'>"
																				+ file.error
																				+ "</div>");
													} else {
														$("[name=postPic]")
																.val(file.url);
														var msg = "<div class='alert alert-success'><strong>上传成功！</strong><br/>{preview}</div>";
														var preview = "";
														var url = ctx + "/"
																+ file.url;
														var thumbnail_url = ctx
																+ "/"
																+ file.thumbnail_url;
														if ($.app
																.isImage(file.name)) {
															preview = "<a href='{url}' target='_blank'><img src='{thumbnail_url}' title='{name}' height='120px'/></a>"
														} else {
															preview = "<a href='{url}' target='_blank'>{name}</a>"
														}
														preview = preview
																.replace(
																		"{url}",
																		url)
																.replace(
																		"{thumbnail_url}",
																		thumbnail_url)
																.replace(
																		"{name}",
																		file.name);
														msg = msg.replace(
																"{preview}",
																preview);
														$(".ajax-upload-view")
																.html(msg);

													}
												});
								var submitBtn = $(this).closest("form").find(
										":submit");
								submitBtn.val(submitBtn.data("value")).prop(
										"disabled", false);
							}
						});
		<c:choose>
		<c:when test="${op eq '删除'}">
		//删除时不验证 并把表单readonly
		$.app.readonlyForm($("#editForm"), false);
		</c:when>
		<c:when test="${op eq '查看'}">
		$.app.readonlyForm($("#editForm"), true);
		</c:when>
		<c:otherwise>
		var validationEngine = $("#editForm").validationEngine();
		<es:showFieldError commandName="m"/>
		</c:otherwise>
		</c:choose>

		$("[name='category.name']")
				.siblings(".add-on")
				.andSelf()
				.click(
						function() {
							$.app
									.modalDialog(
											"参照",
											"${ctx}/wei/circle/category/select/single;domId=categoryId;domName=categoryName",
											{
												width : 600,
												height : 450,
												callback : function() {
													$("[name='category.name']")
															.validationEngine(
																	'validate');
													return true;
												}
											});
						});
		//
		var editor = KindEditor.create('textarea[name="postContent"]', {
			themeType : 'simple',
			uploadJson : '${ctx}/kindeditor/upload',
			fileManagerJson : '${ctx}/kindeditor/filemanager',
			allowFileManager : true,
			afterBlur : function() {
				this.sync();
			}
		});
	});
</script>